package components.dialogs
{
	import SEApp.se_util;
	
	import UIFree.ObjectEvent;
	import UIFree.command.ICommand;
	import UIFree.command.uif_commandManager;
	import UIFree.components.uif_window;
	import UIFree.controls.uif_gbutton;
	import UIFree.controls.uif_vaScrollArea;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import UIUtils.uif_util;
	
	import components.c_window;
	import components.propertyCtrls.c_checkbox;
	import components.propertyCtrls.c_checkboxList;
	import components.propertyCtrls.c_colorBalance;
	import components.propertyCtrls.c_colorCtrl;
	import components.propertyCtrls.c_colorPicker;
	import components.propertyCtrls.c_comboBoxProperty;
	import components.propertyCtrls.c_directCtrl;
	import components.propertyCtrls.c_listCtrl;
	import components.propertyCtrls.c_numberCtrl;
	import components.propertyCtrls.c_propertyBase;
	
	import flash.display.BlendMode;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class c_propertyDialog extends c_window {
		private var _layer: uif_vaScrollArea;
		private var _buttons: uif_ecomponent;
		private var _propertyList: Array;
		private var _propertyCtrls: Array;
		
		public static const PTYPE_NUMBER: String = 'number';
		public static const PTYPE_COLORBALANCE: String = 'colorBalance';
		public static const PTYPE_COLOR: String = 'color';
		public static const PTYPE_COLORPICKER: String = 'colorPicker';
		public static const PTYPE_DIRECT: String = 'direct';
		public static const PTYPE_COMBO: String = 'combo';
		public static const PTYPE_CHECKBOX: String = 'checkbox';
		public static const PTYPE_LIST: String = 'list';
		public static const PTYPE_CHECKBOXLIST: String = 'cblist';

/*		
		public static const BLENDMODE_LIST: Array = [
			{
				caption: BlendMode.NORMAL,
				value: BlendMode.NORMAL
			},{
				caption: BlendMode.ADD,
				value: BlendMode.ADD
			},{
				caption: BlendMode.DIFFERENCE,
				value: BlendMode.DIFFERENCE
			},{
				caption: BlendMode.MULTIPLY,
				value: BlendMode.MULTIPLY
			},{
				caption: BlendMode.ERASE,
				value: BlendMode.ERASE
			},{
				caption: BlendMode.INVERT,
				value: BlendMode.INVERT
			}
		];
*/		
		
		public function c_propertyDialog(parent:uif_component, propertyList: Array, a_title:String='', modal:Boolean=false, canClose:int=2) {
			_propertyList = new Array();
			_propertyCtrls = new Array();
			var size: Number = updatePropertyList(propertyList, _propertyList, _propertyCtrls) + 40;
			size += parent.app.getStyleSpace('window') * 2;;
			super(parent, 250, size + se_util.DEFHEIGHTBUTTON, a_title, modal, canClose);
			createButtons();
			createComponents();
			show();
			addButton('ok', function (e: Event): void {
				close(0, doApply);
			});
			addEvent(Event.CHANGE, doChange);
		}
		
		override protected function startPosition():void {
			magnet(c_window.DOCK_TOP | c_window.DOCK_RIGHT);
		}
		
		override protected function doClose(e: Event): void {
			doCancel();
			super.doClose(e);
		}
		
		protected function doChange(e: Event): void {
		}
		
		protected function doApply(): void {
		}
		
		protected function doCancel(): void {
		}
		
		protected static function updatePropertyList(a_propertyList: Array, a_result: Array, a_propertyCtrls: Array): Number {
			var aheight: Number = 0;
			a_propertyList.forEach(function(property: Object, i: int, arr: Array): void {
				var item: Object = {
					Class	: propertyClass(property.type),
					control	: null
				};
				aheight += item.Class.WEIGHT; 
				a_result.push(uif_util.clone(property));
				a_propertyCtrls.push(item);
			});
			return aheight;
		}
		
		protected static function propertyClass(a_type: String): Class {
			switch (a_type) {
				case PTYPE_NUMBER: return c_numberCtrl;
				case PTYPE_COLORBALANCE: return c_colorBalance;
				case PTYPE_COLOR: return c_colorCtrl;
				case PTYPE_COLORPICKER: return c_colorPicker;
				case PTYPE_DIRECT: return c_directCtrl;
				case PTYPE_COMBO: return c_comboBoxProperty;
				case PTYPE_CHECKBOX: return c_checkbox;
				case PTYPE_LIST: return c_listCtrl;
				case PTYPE_CHECKBOXLIST: return c_checkboxList;
			}
			return c_propertyBase;
		}
		
		protected function createComponents(): void {
			var rect: Rectangle = getInner();
			var pY: Number = title.y + title.height;
			_layer = new uif_vaScrollArea(this, rect.x, rect.y, rect.width, rect.height - _buttons.height);
			_layer.addEvent(Event.CHANGE, doChangeValues);
			_propertyCtrls.forEach(function(item: Object, i: int, arr: Array): void {
				var pClass: Class = item.Class;
				_propertyCtrls[i].control = new (item.Class)(_layer, _propertyList[i], rect.y + pY, item.Class.WEIGHT);
				pY += item.Class.WEIGHT;
			});
		}
		
		protected function get propertyCtrls(): Array {
			return _propertyCtrls;
		}
		
		protected function doChangeValues(e: Event): void {
			e.stopPropagation();
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		protected function createButtons(): void {
			var rect: Rectangle = getInner();
			_buttons = new uif_ecomponent(this, rect.x, rect.y + rect.height - se_util.DEFHEIGHTBUTTON, rect.width, se_util.DEFHEIGHTBUTTON);
		}
		
		public function addButton(caption: String, releaseFunc: Function, a_hint: String=''): uif_gbutton {
			var button: uif_gbutton = se_util.defButton(_buttons, 0, 0, caption, releaseFunc, a_hint);
			refreshButtons();
			return button;
		}
		
		protected function refreshButtons(): void {
			var inner: Rectangle = _buttons.getInner();
			var si: Number = (_buttons.width - _buttons.numChildren * se_util.DEFWIDTHBUTTON) / 2;
			_buttons.forEach(function(button: uif_gbutton): void {
				button.x = inner.x + si;
				button.y = inner.y;
				si += button.width;
			}, uif_gbutton);
		}
		
		public function get values(): Object {
			var result: Array = new Array();
			_propertyCtrls.forEach(function(item: Object, i: int, arr: Array): void {
				result.push(item.control._data);
			});
			return result;
		}
		
		protected function executeCommand(command: ICommand): void {
			cl.workspace.dispatchEvent(new ObjectEvent(uif_commandManager.EXECUTE, command));
		}		
	}
}